package client

import (
	"common/initialization"
	"context"
	"fmt"
	"google.golang.org/grpc"
	mealProduct "meal_products_srv/proto"
)

type MealHandler func(ctx context.Context, client mealProduct.MealProductClient) (interface{}, error)

func MealClient(ctx context.Context, handler MealHandler) (interface{}, error) {
	cf := initialization.Config.ProductServer
	dial, err := grpc.Dial(fmt.Sprintf("%s:%d", cf.Hort, cf.Port), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := mealProduct.NewMealProductClient(dial)
	return handler(ctx, client)
}
func MealProductList(ctx context.Context, req *mealProduct.MealProductListRequest) (*mealProduct.MealProductListResponse, error) {
	client, err := MealClient(ctx, func(ctx context.Context, client mealProduct.MealProductClient) (interface{}, error) {
		list, err := client.MealProductList(ctx, req)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*mealProduct.MealProductListResponse), nil
}
